package com.weishi.project.training.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.utils.StringUtils;
import com.weishi.project.api.vo.ChannelAchievementVo;
import com.weishi.project.training.domain.DynamometerSubsection;
import com.weishi.project.training.domain.Sportsman;
import com.weishi.project.training.domain.TrainingAchievement;
import com.weishi.project.training.mapper.TrainingAchievementMapper;
import com.weishi.project.training.service.IDynamometerSubsectionService;
import com.weishi.project.training.service.ISportsmanService;
import com.weishi.project.training.service.ITrainingAchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.weishi.project.training.mapper.TrainingGroupChannelMapper;
import com.weishi.project.training.domain.TrainingGroupChannel;
import com.weishi.project.training.service.ITrainingGroupChannelService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 训练小组航道Service业务层处理
 */
@Service
public class TrainingGroupChannelServiceImpl extends ServiceImpl<TrainingGroupChannelMapper, TrainingGroupChannel> implements ITrainingGroupChannelService {

    @Autowired
    private TrainingGroupChannelMapper trainingGroupChannelMapper;

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    @Autowired
    private ISportsmanService sportsmanService;

    /**
     * 分段成绩 接口
     */
    @Autowired
    private IDynamometerSubsectionService dynamometerSubsectionService;



    /**
     * 查询训练小组航道列表
     */
    @Override
    public List<TrainingGroupChannel> selectTrainingGroupChannelList(TrainingGroupChannel trainingGroupChannel) {
        List<TrainingGroupChannel> trainingGroupChannels = trainingGroupChannelMapper.selectTrainingGroupChannelList(trainingGroupChannel);
        return trainingGroupChannels;
    }

    @Override
    public TrainingGroupChannel getTrainingGroupChannelDetails(int id) {
        return trainingGroupChannelMapper.getTrainingGroupChannelDetails(id);
    }

    @Override
    public void updateTrainingGroupChannelById(TrainingGroupChannel trainingGroupChannel) {
        trainingGroupChannelMapper.updateTrainingGroupChannelById(trainingGroupChannel);
    }

    @Override
    public List<TrainingGroupChannel> getChannelNameList() {
        return trainingGroupChannelMapper.getChannelNameList();
    }

    @Override
    public int getChannelIdByGroupIdAndChannelNameAndBatch(Map<String, Object> map) {
        return trainingGroupChannelMapper.getChannelIdByGroupIdAndChannelNameAndBatch(map);
    }

    @Override
    public List<TrainingGroupChannel> getTrainingGroupChannelByWeekIdAndTrainingDayId(Map<String, Object> map) {
        return trainingGroupChannelMapper.getTrainingGroupChannelByWeekIdAndTrainingDayId(map);
    }

    @Override
    public List<TrainingGroupChannel> getTrainingGroupChannelByGroupId(int groupId) {
        Map<String, Object> map = new HashMap<>();
        map.put("groupId",groupId);
        return trainingGroupChannelMapper.getTrainingGroupChannelByGroupId(map);
    }

    @Override
    @Transactional
    public void updateTrainingGroupChannelList(TrainingGroupChannel trainingGroupChannel) {
        trainingGroupChannelMapper.updateTrainingGroupChannelList(trainingGroupChannel);
        if(trainingGroupChannel.getPerson()!=null && trainingGroupChannel.getPerson().size()>0){
            trainingAchievementService.deleteTrainingAchievementByChannelId(trainingGroupChannel.getId());
            for(TrainingAchievement trainingAchievement : trainingGroupChannel.getPerson()){
                trainingAchievement.setChannelId(trainingGroupChannel.getId());
                trainingAchievementService.saveOrUpdate(trainingAchievement);
            }
        }
    }

    @Override
    public boolean selectAchievementIsNull(Map<String, Object> map) {
        return trainingGroupChannelMapper.selectAchievementIsNull(map)>0;
    }

    @Override
    public List<ChannelAchievementVo> queryChannelAchievement(Map<String, Object> map) {
        return trainingGroupChannelMapper.queryChannelAchievement(map);
    }

    @Override
    public List<TrainingGroupChannel> qryBestTop10(Map<String, Object> params, boolean setPerson) {
        List<TrainingGroupChannel> list = trainingGroupChannelMapper.qryBestTop10(params);
        if(setPerson){
            for(TrainingGroupChannel trainingGroupChannel : list){
                TrainingAchievement param = new TrainingAchievement();
                param.setChannelId(trainingGroupChannel.getId());
                List<TrainingAchievement> list1 = trainingAchievementService.selectTrainingAchievementList1(param);
                trainingGroupChannel.setPerson(list1);
            }
        }
        return list;
    }

    @Override
    public TrainingGroupChannel getTrainingGroupChannelAndAchievement(Integer channelId) {
        TrainingGroupChannel trainingGroupChannel =  trainingGroupChannelMapper.getTrainingGroupChannelDetails(channelId);
        TrainingAchievement params = new TrainingAchievement();
        params.setChannelId(channelId);

        List<TrainingAchievement> trainingAchievementList = trainingAchievementService.selectTrainingAchievementList1(params);
        for(TrainingAchievement achievement : trainingAchievementList){
            Sportsman s = this.sportsmanService.qrySportsmanByCache(achievement.getSportsmanId());
            achievement.setSportsmanName(s.getName());

            // 判断水上或者陆上
            if(trainingGroupChannel.getFirstLevel().equals("special_training_water")){
                // 水上
                trainingGroupChannel.setSegmentPropellerFrequency(trainingGroupChannel.getPropellerFrequency());
                // 心率
                achievement.setSegmentHeartRate(achievement.getHeartRate());

            }else{
                // 陆上
                /**
                 * 查询 分段数据
                 */
                List<DynamometerSubsection> dynamometerSubsectionList = dynamometerSubsectionService
                        .list(Wrappers.<DynamometerSubsection>lambdaQuery()
                                .eq(DynamometerSubsection::getSportsmanId , achievement.getSportsmanId())
                                .eq(DynamometerSubsection::getGroupId , achievement.getGroupId())
                                .eq(DynamometerSubsection::getTrainingDayId , achievement.getTrainingDayId()));

                // 速度
                double averageSpeed = 0;
                // 取最大心率 计算平均心率
                Integer averageHeartRate = 0;
                // 平均功率
                Integer averagePower = 0;
                // 平均浆频
                Integer averagePropellerFrequency = 0;
                // 分段心率
                String segmentHeartRate = "";
                // 分段浆频
                String segmentPropellerFrequency = "";
                // 分段功率
                String segmentPower = "";
                // 分段瓦特
                String segmentWatt = "";
                // 分段时长
                String segmentLengthTime = "";
                // 分段距离
                String segmentMileage = "";
                if(dynamometerSubsectionList.size() > 0){
                    System.out.println();
                    for(int i = 0; i<dynamometerSubsectionList.size() ;i++){
                        if(StringUtils.isNotNull(dynamometerSubsectionList.get(i).getLengthTime())){
                            averageSpeed += dynamometerSubsectionList.get(i).getSpeed();
                        }
                        System.out.println();
                        if(StringUtils.isNotNull(dynamometerSubsectionList.get(i).getHeartRate())){
                            averageHeartRate+=dynamometerSubsectionList.get(i).getHeartRate();
                        }
                        // 存入浆数
                        trainingGroupChannel.setPulpNumber(dynamometerSubsectionList.get(i).getPulpNumber());
                        trainingGroupChannel.setPower(dynamometerSubsectionList.get(0).getPower());
                        // 计算平均功率
                        averagePower += StringUtils.turnSecond(dynamometerSubsectionList.get(i).getPower());
                        // 存入浆频
                        trainingGroupChannel.setPropellerFrequency(dynamometerSubsectionList.get(i).getPropellerFrequency());
                        // 计算平均浆频
                        averagePropellerFrequency += Integer.parseInt(dynamometerSubsectionList.get(i).getPropellerFrequency());
                        // 峰力
                        trainingGroupChannel.setPeakForce(String.valueOf(dynamometerSubsectionList.get(i).getPeakForce()));
                        // 均力
                        trainingGroupChannel.setAverageForce(String.valueOf(dynamometerSubsectionList.get(i).getAverageForce()));
                        // 平均瓦特
                        trainingGroupChannel.setAverageWatt(String.valueOf(Integer.parseInt(dynamometerSubsectionList.get(0).getWatt())));
                        // 分段心率
                        segmentHeartRate += dynamometerSubsectionList.get(i).getHeartRate() + " ";
                        // 分段浆频
                        segmentPropellerFrequency += dynamometerSubsectionList.get(i).getPropellerFrequency() + " ";
                        // 分段功率
                        segmentPower += dynamometerSubsectionList.get(i).getPower() + " ";
                        // 分段瓦特
                        segmentWatt += dynamometerSubsectionList.get(i).getWatt() + " ";
                        // 速度
                        trainingGroupChannel.setSpeed(dynamometerSubsectionList.get(i).getSpeed());
                        if(dynamometerSubsectionList.get(i).getTrainingMode() == 0){
                            // 分段时长
                            segmentLengthTime += dynamometerSubsectionList.get(i).getLengthTime() + " ";
                            // 存储分段成绩
                            String segmentedTiming = segmentLengthTime.substring(0 , segmentLengthTime.length()-1);
                            trainingGroupChannel.setSegmentedTiming(segmentedTiming);
                        }else{
                            // 分段距离
                            segmentMileage += dynamometerSubsectionList.get(i).getMileage() + " ";
                            String segmentedTiming = segmentMileage.substring(0 , segmentMileage.length()-1);
                            trainingGroupChannel.setSegmentedTiming(segmentedTiming);
                        }
                    }
                    Double averageSpeed2 = averageSpeed/dynamometerSubsectionList.size();
                    Long a =Math.round(averageSpeed2);
                    Double c = a.doubleValue();
                    trainingGroupChannel.setAverageSpeed(c.toString());
                    // 存储平均心率
                    trainingGroupChannel.setAverageHeartRate(averageHeartRate);
                    // 存储平均功率
                    Integer averagePowers = averagePower/dynamometerSubsectionList.size();
                    trainingGroupChannel.setAveragePower(StringUtils.millisecondRotationTime(averagePowers));
                    // 平均浆频
                    trainingGroupChannel.setAveragePropellerFrequency(averagePropellerFrequency/dynamometerSubsectionList.size());
                    // 存储分段心率
                    achievement.setSegmentHeartRate(segmentHeartRate.substring(0 , segmentHeartRate.length()-1));
                    // 存储分段浆频
                    trainingGroupChannel.setSegmentPropellerFrequency(segmentPropellerFrequency.substring(0 , segmentPropellerFrequency.length()-1));
                    // 存储分段功率
                    trainingGroupChannel.setSegmentPower(segmentPower.substring(0 , segmentPower.length()-1));
                    // 存储分段瓦特
                    trainingGroupChannel.setSegmentWatt(segmentWatt.substring(0 , segmentWatt.length()-1));
                    // 存储 训练课 id
                    trainingGroupChannel.setTrainingDayId(achievement.getTrainingDayId());
                }




            }
        }
        trainingGroupChannel.setPerson(trainingAchievementList);

        return trainingGroupChannel;
    }

    @Override
    public boolean add(TrainingGroupChannel trainingGroupChannel) {
        boolean ret = this.save(trainingGroupChannel);
        if(trainingGroupChannel.getPerson()!=null && trainingGroupChannel.getPerson().size()>0){
            for(TrainingAchievement trainingAchievement : trainingGroupChannel.getPerson()){
                trainingAchievement.setChannelId(trainingGroupChannel.getId());
                trainingAchievement.setCtime(new Date());
                // 接收 分段心率
                List<TrainingAchievement> segmentHeartRate = trainingGroupChannel.getPerson();
                String segmentHeartRate2 = "";
                for (int i = 0; i<segmentHeartRate.size(); i++){
                    segmentHeartRate2 += segmentHeartRate.get(i).getSegmentHeartRate() + " ";
                }
                trainingAchievement.setHeartRate(segmentHeartRate2); // 存储分段心率

                trainingAchievementService.saveOrUpdate(trainingAchievement);
            }
        }
        return ret;
    }
}